/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/10 11:08
 * @Copyright
 */
package IO2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/*
jab\va.io.FileInputStream:
1.字节输入流：王能的，任何类型的文件都可以采用这个流来读
2.字节的方式：完成输入的操作，完成读的操作（硬盘-->内存）
 */
public class FileInputStreamTest1 {
    public static void main(String[] args) {
        FileInputStream fis =null;
        //文件路径采用绝对路径：从盘符开始
        try {
             fis = new FileInputStream("E:\\code\\TestDemo\\src\\demo\\IO2\\temp");
             //开始读
            int readData = fis.read();
            System.out.println(readData); //ASCII值97

            readData=fis.read();
            System.out.println(readData); //98

            readData=fis.read();
            System.out.println(readData); //99

            readData=fis.read();
            System.out.println(readData); //100

            readData=fis.read();
            System.out.println(readData); //101

            readData=fis.read();
            System.out.println(readData); //102

            //已经读到文件的末尾了，再读的时候读取不到任何数据，返回-1
            readData=fis.read();
            System.out.println(readData); //-1

            readData=fis.read();
            System.out.println(readData); //-1

            readData=fis.read();
            System.out.println(readData); //-1

        }catch (FileNotFoundException e){
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            {
                //在finally语句块中，确保流一定关闭
                if (fis !=null){ //避免空指针异常
                    //关闭流的前提是：流不是空
                    try {
                        fis.close();
                    }catch (IOException e){
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
}
